#include "timer.h"

#include <Windows.h>
#include <stdio.h>

extern void setupGame();
extern void updateGame( float fTimeDelta );
extern void drawGame( float fTimeDelta );
extern void shutdownGame();

int setupWindow();
int setup( void )
{
	setupWindow();
	// we don't make any more changes to these

	setupGame();
	return 0;
};

void update( float fTimeDelta )
{
	updateGame( fTimeDelta );
};

void draw( float fTimeDelta )
{
	drawGame( fTimeDelta );
};

static int requestedShutdown = 0;

void shutdownApp()
{
	requestedShutdown = 1;
};

static HWND g_hWindowHandle=0;

int main( void )
{
	setup();

	STimer frameTimer;

	printf( "Showing window and starting main loop...\n" );
	ShowWindow( g_hWindowHandle, SW_SHOW );
	UpdateWindow( g_hWindowHandle );	

	while( true )
    {
		MSG msg = {0};
		while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )	// Process Windows messages
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		if(GetAsyncKeyState(VK_ESCAPE) || msg.message == WM_QUIT) // Check if exit was requested.
		{
			shutdownGame();
		}
		//
		if(requestedShutdown) // end loop if requested
		{
			printf("Exiting...\n", frameTimer.LastTime);
			break;
		}
		frameTimer.Frame();
		printf("Frame time: %f\n", frameTimer.LastTime);
        
		// Update and draw frame.
		update(frameTimer.LastTime);
        draw(frameTimer.LastTime);

		Sleep(50);
    };

	UnregisterClass(L"micro3d",GetModuleHandle(0));
    return 0;
}

LRESULT __stdcall MainWindowProc( HWND hWnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam )
{
	switch( uMsg )
	{
	case WM_CLOSE:
		DestroyWindow(g_hWindowHandle);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_QUIT:
		break;
	}
	return DefWindowProc( hWnd, uMsg, wParam, lParam );
};

int setupWindow( void )
{
	int nPositionX = 10, nPositionY = 10 , nWidth = 800, nHeight = 600;
	// Default to a window with a client area rectangle of width x height.
	RECT r = { nPositionX, nPositionY, nWidth, nHeight };

	printf( "Window position: x=%i. y=%i\n"
		"Window size: x=%i. y=%i\n", nPositionX, nPositionY, nWidth, nHeight );

	WNDCLASS wc;
	wc.style			= NULL; // CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= MainWindowProc; 
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= GetModuleHandle(0);
	wc.hIcon			= LoadIcon(0, IDI_APPLICATION);
	wc.hCursor			= LoadCursor(0, IDC_ARROW);
	wc.hbrBackground	= CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	wc.lpszMenuName		= 0;
	wc.lpszClassName	= L"micro3d";
	unsigned int dwStyle	= WS_OVERLAPPED | WS_THICKFRAME | WS_BORDER | WS_MAXIMIZEBOX |
								WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX;
	if( !RegisterClass(&wc) )
	{
		printf( "Failed to register WNDCLASS \"micro3d\".\n" );
		return -1;
	}
	else
	{
		printf( " \"micro3d\" WNDCLASS registered successfully.\n" );
	}

	g_hWindowHandle = CreateWindow(	L"micro3d", L"micro3d runtime.", 
								dwStyle, nPositionX, nPositionY, 
								r.right + GetSystemMetrics( SM_CXFRAME )*2, 
								r.bottom + GetSystemMetrics( SM_CYFRAME ) *2
								+ GetSystemMetrics( SM_CYCAPTION ),
								0, 0, wc.hInstance, 0
							); 

	if( 0 == g_hWindowHandle )
	{
		printf( "CreateWindow FAILED!" );
		return -1;
	}

	return 0;
}

